package com.kobe.tokendemo.exception;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.kobe.tokendemo.ResultWrapper;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创建 JYQ  on  2018/11/1,16:30
 */
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView view = new ModelAndView();
        if (ex instanceof NullPointerException) {
            view.setViewName(String.valueOf(new MappingJackson2JsonView()));
            view.addObject("code", 401);
            view.addObject("status", false);
            view.addObject("message", "失败");
        } else {
            view.addObject("message", "服务器出错了");
        }
        return view;
    }
}